<template>
    <div class="cmsCatDialog" v-if="cmsCatDialogVisible">
        <el-dialog :close-on-click-modal="isModelClose" :style="dialogStyle" :before-close="close" title="更新内容管理" v-if="cmsCatDialogVisible" :visible.sync="cmsCatDialogVisible">
            <el-form ref="brandForm" :model="form" :rules="rules">
                <el-form-item label="内容分类名称" :label-width="formLabelWidth" prop="name">
                    <el-input v-model="form.name" autocomplete="off"></el-input>
                </el-form-item>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button @click="close">取 消</el-button>
                <el-button type="primary" @click="submitForm('brandForm')">确 定</el-button>
            </div>
        </el-dialog>
    </div>
</template>
<script lang="ts">
    import { Component, Emit, Prop, Vue } from "vue-property-decorator";
    import AdminApiActions from "@/components/api/admin-api-actions";
    import $ from "jquery";
    @Component
    export default class BrandDialog extends Vue {
        @Prop() params: any;
        public paramData: any = {};
        public cmsCatDialogVisible: boolean = true;
        public dialogStyle: any = {};
        public isModelClose: boolean = false;
        public imageUrl: any = "";
        public form: any = {
            name: "",
            letter: "",
            image: "",
        };
        //表单校验
        public rules: any = {
            name: [{ required: true, message: "请输入名称", trigger: "blur" }],
            letter: [{ required: true, message: "请输入Letter", trigger: "blur" }],
        };
        public handleAvatarSuccess(data) {
            this.imageUrl = data.url;
            this.paramData = {
                orgUrl: data.url,
            };
            this.form.image = data.url;
        }
        public beforeAvatarUpload() {}
        public formLabelWidth: any = "140px";
        //创建时调用
        async created() {
            this.cmsCatDialogVisible = true;
            this.$nextTick(() => {
                this.centerDialog();
            });
        }

        @Emit("close")
        public close() {
            this.cmsCatDialogVisible = false;
        }

        @Emit("confirm")
        public confirm() {
            this.cmsCatDialogVisible = false;
        }
        //提交表单
        public submitForm(formName) {
            (this.$refs[formName] as any).validate((valid) => {
                if (valid) {
                    if (this.params.type == "add") {
                        this.saveBrand();
                    } else {
                        this.updateBrand();
                    }
                } else {
                    return false;
                }
            });
        }
        //保存分组
        public async saveBrand() {
            let apiActions = new AdminApiActions(this);
            await apiActions.addBrand(this.form);
            this.confirm();
        }
        //编辑分组
        public async updateBrand() {
            let apiActions = new AdminApiActions(this);
            await apiActions.updateBrand(this.form);
            this.confirm();
        }

        public centerDialog() {
            this.$nextTick(() => {
                let dialogHeight = $(".cmsCatDialog .el-dialog").height();
                let windowHeight = $(window).height();
                let ss = (windowHeight - dialogHeight) / 2;
                this.dialogStyle = {
                    top: ss + "px",
                };
            });
        }
    }
</script>
<style lang="less">
    .cmsCatDialog {
        position: absolute;
        width: 100%;
        height: 100%;

        .el-dialog__body {
            padding-bottom: 0px;
            overflow: auto;
        }

        .el-form-item__label {
            width: 108px !important;
        }

        .el-form-item__content {
            margin-left: 108px !important;
        }

        .el-dialog {
            width: 30% !important;
            margin-top: 0px !important;

            .dialog-footer {
                display: flex;
                justify-content: center;
            }
        }
    }
</style>